package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.AccountHistoryModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.TimeUtils;

import java.util.List;

/**
 * Created by wangyang on 2016/6/14.
 */
public class TransactionRecordAdapter extends BaseRecyclerAdapter<AccountHistoryModel.ListBean, TransactionRecordAdapter.AccountViewHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private Context context;

    public TransactionRecordAdapter(Context context, List<AccountHistoryModel.ListBean> data) {
        super(context, data);
        this.context = context;

    }

    @Override
    public AccountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_account_history, parent, false);
        return new AccountViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AccountViewHolder holder, int position) {
        holder.bindData(position);
    }


    @Override
    public long getHeaderId(int position) {
        return data.get(position).getSort();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.header_item, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView tv = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);
        String localData = TimeUtils.formatTimeDifference5(System.currentTimeMillis());

        MyLog.i("yang", "data.get(position).getMyGroupName()==" + data.get(position).getMyGroupName());

        if (localData.equals(data.get(position).getMyGroupName())) {
            tv.setText("本月");
        } else {
            tv.setText(data.get(position).getMyGroupName());
        }


    }

    public class AccountViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private TextView tv_time_zhou;
        private TextView tv_time_data;
        private ImageView img_paid_style_icon;
        private TextView tv_paid_in_out;
        private TextView tv_paid_des;
        private TextView tv_cost_content;
        private TextView tv_cost;


        public AccountViewHolder(View itemView) {
            super(itemView);
            tv_time_zhou = (TextView) itemView.findViewById(R.id.tv_time_zhou);
            tv_time_data = (TextView) itemView.findViewById(R.id.tv_time_data);
            img_paid_style_icon = (ImageView) itemView.findViewById(R.id.img_paid_style_icon);
            tv_paid_in_out = (TextView) itemView.findViewById(R.id.tv_paid_in_out);
            tv_paid_des = (TextView) itemView.findViewById(R.id.tv_paid_des);
            tv_cost_content = (TextView) itemView.findViewById(R.id.tv_cost_content);
            tv_cost = (TextView) itemView.findViewById(R.id.tv_cost);

        }

        public void bindData(final int position) {
            String eventName = data.get(position).getEventName();
            if (data.get(position).getDebtorOwnerId() > 0) {
                tv_cost_content.setVisibility(View.VISIBLE);
                tv_cost.setVisibility(View.VISIBLE);
                tv_cost_content.setText("借余额人：" + data.get(position).getDebtorName() + " " + data.get(position).getDebtorPhone() + " " + data.get(position).getDebtorLicenseNumber());
            } else {
                tv_cost_content.setVisibility(View.GONE);
                tv_cost.setVisibility(View.GONE);
            }
            try {
                if (data.get(position).getInOutType().equals("in")) {
                    img_paid_style_icon.setImageResource(R.mipmap.icon_rechage);
                    tv_paid_in_out.setText("+￥" + String.format("%.2f", data.get(position).getInOutAmount()));
                    if (eventName.equals("购买套餐")) {
                        tv_paid_des.setText("套餐增送");
                    } else {
                        tv_paid_des.setText(data.get(position).getEventName());
                    }
                } else if (data.get(position).getInOutType().equals("out")) {
                    img_paid_style_icon.setImageResource(R.mipmap.icon_paid);
                    tv_paid_in_out.setText("-￥" + String.format("%.2f", data.get(position).getInOutAmount()));
                    if (eventName.equals("购买套餐")) {
                        tv_paid_des.setText("购买套餐");
                    } else {
                        tv_paid_des.setText(data.get(position).getEventName());
                    }
                }
                String Z = TimeUtils.TransactionRecordformatTimeZ(data.get(position).getEventTime());
                String D = TimeUtils.TransactionRecordformatTimeD(data.get(position).getEventTime());

                tv_time_zhou.setText(Z);
                tv_time_data.setText(D);

            } catch (Exception e) {

            }
        }
    }


}
